package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.req.Action;
import com.lutzyt.delivery.service.ActionService;
import lombok.Getter;
import lombok.Setter;

/**
 * @author zyt
 */
@Setter
@Getter
public class ActionServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements ActionService<T> {
  private String on = "on";
  private String off = "off";
  private String onStr = "已激活，请不要重复操作！";
  private String offStr = "已停用，请不要重复操作！";

  @Override
  public void action(Action action) {
    final T query = this.query().eq("state", action.getState()).eq("id", action.getId()).one();
    if (query != null && query.equals(getOn())) {
      throw new RuntimeException(getOnStr());
    } else if (query != null && query.equals(getOff())) {
      throw new RuntimeException(getOffStr());
    }
    this.update().set("state", action.getState()).eq("id", action.getId()).update();
  }
}
